<?php
namespace App\Http\Controllers\Admin;
use App\http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends Controller{
    public function index(){
        return view('admin/index');
        //视图文件是 resources/views/admin/index.blade.php
    }
    public function todo_adduser(Request $request){
        $data = array(
            "user_name"=>$request->user_name,
            "user_pwd"=>md5($request->user_pwd),
            "user_email"=>$request->user_email,
            "user_address"=>$request->user_address,
            "user_tel"=>$request->user_tel,
            "user_addtime"=>strtotime($request->user_addtime),
        );
        $res = DB::table("user")->insert($data);
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"添加成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
    public function toget_user_list(Request $request){
        $user_p = DB::table('user')
        ->orderBy("user_addtime", "DESC");
        //echo  $request->user_name;die;
        if($request->user_name){
            $user_p = $user_p->where('user_name','like','%'.$request->user_name.'%');
        }
        if($request->start){
            $user_p = $user_p->where('user_addtime','>',strtotime($request->start));
        }
        if($request->end){
            $user_p = $user_p->where('user_addtime','<',strtotime($request->end));
        }
       $user_p=$user_p->paginate(5);
        //dd($user_p);die;
        $user = $user_p->items();
        $current = $user_p->currentPage();
        $a = ($current-1)*5+1;
        echo '
        <table class="layui-table">
            <thead>
                <tr>
                    <th>
                    <input type="checkbox" name="all" value="" />
                    </th>
                    <th>编号</th>
                    <th>会员名称</th>
                    
                    <th>用户邮箱</th>
                    <th>用户地址</th>
                    <th>用户电话</th>
                    <th>添加时间</th>
                    <th >操作</th>
                </tr>
            </thead>
            <tbody>
        ';

//        echo '<pre>';
//         print_r($admin);
//        echo '</pre>';
        foreach ($user as $value){
            $user_time = date('Y-m-d',$value->user_addtime);
            //echo $admin_time;
            echo '
            <tr>
                <td>
                <input type="checkbox" name="id[]" value="'.$value->user_id.'"  />
                </td>
                <td>'.$a++.'</td>
                <td>'.$value->user_name.'</td>
                
                <td>'.$value->user_email.'</td>
                <td>'.$value->user_address.'</td>
                <td>'.$value->user_tel.'</td>
                <td>'.$user_time.'</td>
                <td class="td-manage">
                    <a title="查看"  onclick="x_admin_show('."'编辑'".','."'/admin/user_edit/$value->user_id'".')" href="javascript:;">
                        <i class="layui-icon">&#xe63c;</i>
                    </a>
                    <a title="删除" onclick="member_del(this,'.$value->user_id.')" href="javascript:;">
                        <i class="layui-icon">&#xe640;</i>
                    </a>
                    <a onclick="x_admin_show(\'修改密码\',\'/admin/user_editpwd/'.$value->user_id.'\',600,400)" title="修改密码" href="javascript:;">
                        <i class="layui-icon">&#xe631;</i>
                     </a>
                </td>
            </tr>
            ';
        }
        echo '
            </tbody>
            </table>
            <div class="page">'.$user_p->render().'
            </div>
        ';
        // echo 13213;
        //show_msg('3213','/');
    }
    public function toget_user_count(){
        $count = DB::table('user')->count();
        echo $count;die;
    }
    public function user_list(){



        return view('admin/user/user_list');
    }
    public function todo_user_edit(Request $request){

        $data = array(
            "user_name"=>$request->user_name,
            "user_email"=>$request->user_email,
            "user_address"=>$request->user_address,
            "user_tel"=>$request->user_tel,
            "user_addtime"=>strtotime($request->user_addtime),
        );
        $res = DB::table("user")->where('user_id',"$request->user_id")->update($data);
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"编辑成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }


    }
    public function user_add(){

        // echo 13213;
        //show_msg('3213','/');
        return view('admin/user/user_add');
    }
    public function user_edit($user_id){
        $user = DB::table('user')->where('user_id',"$user_id")->first();
        $user->user_addtime = date('Y-m-d',$user->user_addtime);
        return view('admin/user/user_edit',["user"=>$user]);
    }
    public function todo_user_del(Request $request){
        $user_id = $request->user_id;
        $res = DB::table('user')->where('user_id',"$user_id")->delete();
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"删除成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
    public function todo_user_delall(Request $request){
       // echo json_encode($request->arr);
        $res = DB::table('user')->whereIn("user_id",$request->arr)->delete();
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"删除成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
    public function user_editpwd($user_id){
        $user = DB::table('user')->where('user_id',"$user_id")->first();

        return view('admin/user/user_editpwd',["user"=>$user]);
    }
    public function todo_user_editpwd(Request $request){
        $user_id = $request->user_id;
        $data = array(
            "user_pwd"=>md5($request->user_pwd),
        );
        $res = DB::table("user")->where('user_id',"$request->user_id")->update($data);
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"修改成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
}